package com.xiaoshuidi.cloud.module.iot.dal.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiaoshuidi.cloud.module.iot.dal.model.doorLock.DoorLockOperationRecord;
import com.xiaoshuidi.cloud.module.iot.dal.model.doorLock.DoorLockReportRespVO;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceOperation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <br>
 *
 * @author Porter
 * create by 2019/7/5
 * @Emial Porter.Wang@ixiaoshuidi.com
 */

@Mapper
public interface DeviceOperationMapper extends BaseMapper<TbDeviceOperation> {

    /**
     * 根据设备ID查询开门记录数量
     *
     * @param deviceNo
     * @return
     */
    @Select("        SELECT COUNT(id) " +
            "        FROM tb_device_operation t " +
            "        WHERE t.device_no=#{deviceNo} " +
            "        AND t.`status`=1 AND t.op_type=1 and deleted=0")
    int getDeviceOpenRecordNumByDeviceNo(String deviceNo);

    /**
     * 获取门锁开门记录
     *
     * @return
     */
    @Select("SELECT " +
            "tdo.member_name name, " +
            "tdo.op_time time " +
            "FROM " +
            "tb_device_operation tdo" +
            " where ${ew.sqlSegment}")
    List<DoorLockReportRespVO> getOpenDoorRecord(@Param("ew") QueryWrapper<?> wrapper);

    /**
     * 获取门锁操作记录
     *
     * @return
     */
    @Select("SELECT " +
            "  tdo.creator userId, " +
            "  tdo.op_type opType, " +
            "  tdo.op_time time, " +
            "  tdo.member_name thirdPassId " +
            "  FROM " +
            "  tb_device_operation tdo " +
            " where ${ew.sqlSegment}")
    List<DoorLockOperationRecord> getOperationRecord(@Param("ew") QueryWrapper<?> wrapper);

}
